/**
 * 产品
 * 校验规则
 */

const { query, checkSchema } = require("express-validator");

// 通用规则
const commonRules = {
  category_id: {
    in: ["body"],
    toInt: true,
    isInt: {
      min: 1,
    },
    errorMessage: "请输入产品分类ID",
    trim: true,
  },
  title: {
    in: ["body"],
    notEmpty: true,
    trim: true,
    errorMessage: "请输入产品名称",
  },
  content: {
    in: ["body"],
    trim: true,
    errorMessage: "请输入产品详情",
    customSanitizer: {
      options: (value) => value || "",
    },
  },
  params: {
    in: ["body"],
    trim: true,
    errorMessage: "请输入产品参数",
    customSanitizer: {
      options: (value) => value || "",
    },
  },
  video_url: {
    in: ["body"],
    trim: true,
    errorMessage: "请输入产品视频链接",
    customSanitizer: {
      options: (value) => value || "",
    },
  },
  specs_book_url: {
    in: ["body"],
    trim: true,
    errorMessage: "请输入产品说明书链接",
    customSanitizer: {
      options: (value) => value || "",
    },
  },
  product_imgs: {
    in: ["body"],
    trim: true,
    errorMessage: "请输入产品图片链接",
    customSanitizer: {
      options: (value) => value || "",
    },
  },
};

const productValidation = {
  // 产品列表
  getProductList_validation: [query("page").default(1).toInt().isInt({ min: 1 }).withMessage("请输入页码"), query("limit").default(10).toInt().isInt({ min: 1 }).withMessage("请输入每页数量")],
  // 产品详情
  getProductDetail_validation: checkSchema({
    product_id: {
      in: ["query"],
      toInt: true,
      isInt: {
        min: 1,
      },
      errorMessage: "请输入产品ID",
    },
  }),
  // 新增产品
  addProduct_validation: checkSchema({ ...commonRules }),
  // 编辑产品
  editProduct_validation: checkSchema({
    product_id: {
      in: ["body"],
      toInt: true,
      isInt: {
        min: 1,
      },
      errorMessage: "请输入产品ID",
    },
    ...commonRules,
  }),
};

module.exports = productValidation;
